//
//  Date++.swift
//  YXRSmartDeviceDemo
//
//  Created by run on 2024/6/26.
//

import Foundation

private let formatter: DateFormatter = {
    let f = DateFormatter()
    f.timeZone = TimeZone(abbreviation: "UTC+8")
    f.dateFormat = "yyyy-MM-dd HH:mm:ss"
    return f
}()

private let ymdFormatter: DateFormatter = {
    let f = DateFormatter()
    f.timeZone = TimeZone(abbreviation: "UTC+8")
    f.dateFormat = "yyyy-MM-dd"
    return f
}()

private let ymdssFormatter: DateFormatter = {
    let f = DateFormatter()
    f.formatterBehavior = .behavior10_4
    f.dateFormat = "HH:mm:ss.SSS"
    f.timeZone = TimeZone(abbreviation: "UTC+8")
    return f
}()

public extension Date {
    var yMdss: String {
        return ymdssFormatter.string(from: self)
    }

    var yMds: String {
        return formatter.string(from: self)
    }

    var yMd: String {
        return ymdFormatter.string(from: self)
    }

    internal var timestamp: UInt32 {
        return UInt32(timeIntervalSince1970)
    }

    internal var nextYear: Date {
        return Calendar.current.date(byAdding: .year, value: 1, to: self)
            ?? (self + TimeInterval(365 * 24 * 3600))
    }
}

public extension UInt32 {
    func toDate() -> Date {
        let t = TimeInterval(self)
        //        return startDate + t
        return Date(timeIntervalSince1970: t)
    }
}
